package com.dingxin.ship_together_api.config;

import cn.dev33.satoken.interceptor.SaInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.Arrays;

/**
 * Author: ctj
 * Date: 2022-08-03
 * Time: 17:41
 */
@Configuration
@EnableWebMvc
public class SaTokenConfigure implements WebMvcConfigurer {
    //放行路径
    private static final String[] EXCLUDE_PATH_PATTERNS = {
        // Swagger
        "**/swagger-ui.html",
        "/swagger-resources/**",
        "/webjars/**",
        "/v2/**",
        "/swagger-ui.html/**",
        "/doc.html/**",
        "/error",
        "/favicon.ico",
        "sso/auth",
        "/csrf"
    };
    // 注册Sa-Token的注解拦截器，打开注解式鉴权功能
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 注册注解拦截器，并排除不需要注解鉴权的接口地址 (与登录拦截器无关)
        registry.addInterceptor(new SaInterceptor())
            .addPathPatterns("/**")
            .excludePathPatterns(Arrays.asList(EXCLUDE_PATH_PATTERNS));
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
    }
}
